package com.youdian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.youdian.R;
import com.youdian.activity.FoodsActivity;
import com.youdian.entity.Business;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by moker on 2017/12/14.
 */

public class BusinessAdapter extends RecyclerView.Adapter<BusinessAdapter.HoldView> {

    private List<Business> mData;
    private Context mContext;

    public BusinessAdapter(List<Business> data, Context context){
        mData = data;
        mContext = context;
    }

    @Override
    public HoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_business,null);
        HoldView holdView = new HoldView(view);
        return holdView;
    }

    @Override
    public void onBindViewHolder(HoldView holder, int position) {
        final Business business = mData.get(position);

        holder.textName.setText(business.getBusinessname());

        holder.textPhone.setText(business.getPhone());

        holder.textAddress.setText(business.getAddress());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, FoodsActivity.class);
                intent.putExtra("businessId",business.getId());
                mContext.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mData.size();
    }


    public static class HoldView extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_business_name)
        TextView textName;
        @BindView(R.id.tv_business_phone)
        TextView textPhone;
        @BindView(R.id.tv_business_address)
        TextView textAddress;

        public HoldView(View itemView) {
            super(itemView);
            //根据onCreateViewHolder的HoldView所添加的xml布局找到空间
            ButterKnife.bind(this,itemView);
        }
    }
}
